<?php
require_once('Zend/Pdf/Page.php');

class Gorilla_Pdf_Page extends Zend_Pdf_Page {
	
	protected $_document;
	protected $_margin;
	protected $_x = 0;
	protected $_y = 0;

	protected $_defaultOptions;
	protected $_options;

	public function __construct($param1, $param2 = null, $param3 = null) {
		parent::__construct($param1, $param2, $param3);

		$this->_defaultOptions = $this->getDocument()->getOptions();
		$this->_setOptions($options);
		
		$this->setFont( Zend_Pdf_Font::fontWithName( $this->getOption('font') ) );

		$this->init();
	}

	public function init() {
	
	
	}

	public function setOptions(array $options) {
 		$diff = array_diff_key($options, $this->_defaultOptions);
        if ($diff) {
            list($key, $val) = each($diff);
            require_once 'Gorilla/Pdf/Exception.php';
            throw new Gorilla_Pdf_Exception("Unknown Gorilla_Pdf option: $key");
        }

        foreach ($this->_defaultOptions as $key => $val) {
            if (!array_key_exists($key, $options)) {
                $options[$key] = $val;
            }
        }

        $this->_options = $options;
	}

	public function getOptions() {
		return $this->_options;
	}
	
	public function getOption($key) {
		return $this->_options[ $key ];
	}

    public function drawInfoBox($header, $lines, $x, $y, $width, $height) {
        $font_size = $this->getFontSize();
        //-- Draw the box
        $this->drawRectangle($x, $this->getHeight()-$y, $x+$width, $this->getHeight()-$y-$height, $fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);
        //-- Draw the header bottom
        $this->drawLine($x, $this->getHeight()-$y-($font_size*2), $x+$width, $this->getHeight()-$y-($font_size*2));
        //-- Draw the header text
        $this->drawText($header, $x+($font_size / 2), $this->getHeight()-$y-$font_size-($font_size/4));
        $this->drawMultilineText($lines, $x+3, $y+($font_size*3));
    }

    public function drawMultilineText($lines, $x, $y) {
        $y = $this->getHeight() - $y;
        $font_size = $this->getFontSize();
        foreach($lines as $i=>$line) {
            $this->drawText($line, $x+2, $y-($font_size * 1.2 * $i));
        }
    }

    public function getVariableText($str, $x, $y, $max_width) {
        $y = $this->getHeight() - $y;
        $font = $this->getFont();
        $font_size = $this->getFontSize();
        //-- Find out each word's width
        $words = explode(' ', $str);
        $words_lens = array();
        $em = $font->getUnitsPerEm();
        $space_size = array_sum($font->widthsForGlyphs(array(ord(' ')))) / $em * $font_size;
        foreach($words as $i=>$word) {
            $word .= ' ';
            $glyphs = array();
            foreach(range(0, strlen($word)-1) as $i) {
                $glyphs[] = ord($str[$i]);
            }
            $words_lens[] = array_sum($font->widthsForGlyphs($glyphs)) / $em * $font_size;
        }
        //-- Push words onto lines to be drawn.
        $y_inc = $y;
        $x_inc = 0;
        $lines = array();
        $line = array();
        $i = 0;
        $max_length = count($words);
        while($i < $max_length) {
            if(($x_inc + $words_lens[$i]) < $max_width) {
                $x_inc += $words_lens[$i] + $space_size;
                $line[] = $words[$i];
            }
            else {
                $lines[] = array($line, $x, $y_inc);
                $y_inc -= $font_size;
                $x_inc = 0;
                $line = array();
                $line[] = $words[$i+1];
                $i++;
            }
            $i++;
        }
        unset($words_lens);
        $lines[] = array($line, $x, $y_inc);
        //var_dump($lines); echo "<br/>";
        return array('width'=>$max_width, 'height'=>($font_size * count($lines)), 'lines'=>$lines);
    }

    public function drawVariableText($str, $x, $y, $max_width, $align='left') {
        $text = $this->getVariableText($str, $x, $y, $max_width);
        foreach($text['lines'] as $line) {
            list($str, $x, $y) = $line;
            $x_pos = $x;
            if($align == 'right') {
                $len = $this->calculateTextWidth(implode(' ', $str));
                $x_pos += $max_width - $len;
            }
            else if ($align == 'center') {
                $len = $this->calculateTextWidth(implode(' ', $str));
                $x_pos += ($max_width - $len) / 2;
            }
            $this->drawText(implode(' ', $str), $x_pos, $y);
        }
        return array('width'=>$max_width, 'height'=>$text['height']);
    }

    public function calculateTextWidth($str) {
        $font = $this->getFont();
        $font_size = $this->getFontSize();
        //-- Find out each word's width
        $em = $font->getUnitsPerEm();
        $glyphs = array();
        foreach(range(0, strlen($str)-1) as $i) {
            $glyphs[] = ord($str[$i]);
        }
        return array_sum($font->widthsForGlyphs($glyphs)) / $em * $font_size;
    }


    /**
     * Add $x pixels to the current x-axis position
     * @param int $x
     * @return Gorilla_Pdf
     */
    public function addX($x) {
    	$this->_x+=$x;
        return $this;
    }

    /**
     * Add $y pixels to the current y-axis position
     * @param int $y
     * @return Gorilla_Pdf
     */
    public function addY($y) {
    	$this->_y+=$y;
        return $this;
    }

    public function drawText($text) {
        return $this;
    }

    public function drawHline($x) {
        return $this;
    }

    public function setLineHeight($pixels) {
        return $this;
    }
    
    public function setX($x) {
    	$this->_x = $x;
        return $this;
    }

    public function setY($y) {
    	$this->_y = $y;
        return $this;
    }

    public function setAlign($align) {
    	$this->_align = $align;
        return $this;
    }

    public function setWrap($width) {
       $this->_wrap = $wrap;
       return $this;
    }
	
	public function setDocument($document) {
        $this->_document = $document;
        return $this;
    }
    
	public function setMargin($left, $top = null, $right = null, $bottom = null) {
    
    	$this->_margin = new stdClass;
    	
    	$this->_margin->left = $left;
    	$this->_margin->top = isset($top)?$top:$this->_margin->left;
    	
    	$this->_margin->right = isset($right)?$right:$this->_margin->left;
    	$this->_margin->bottom = isset($bottom)?$bottom:$this->_margin->right;
    	
    	
        return $this;
    }

    public function getMargin() {
        return $this->_margin;
    }

 	public function getDocument() {
        return $this->_document;
    }

}

